//Author: Zhengwei Pu & Lu Xu
// Term project for ECE59500

//If run this game in an operation system based on Chinese, the buttons of the dialog will show Chinese!
//
//But if in English OS, it also works well!
//
//The language of this game is based on your operation system. 


import java.awt.BorderLayout;
import java.awt.Container;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

/*
 * Main game class
 */
public class SnakeGame extends JFrame{
	private Canvas gameCanvas;//the Canvas of game
	private ControlPanel controlPanel;//controlPanel of the game
	private Snake snake;//snake
	private int level,score;//level,score of the game
	private boolean gameOver;//check if the game is over
        
	public SnakeGame(){
                setTitle("Greedy Snake Game");         //show the title of this game         	 
                
		gameCanvas = new Canvas();
		controlPanel = new ControlPanel(this);

		Container contentPane = getContentPane();
		contentPane.add(gameCanvas,BorderLayout.CENTER);//add the gamecanvas to the center of the window boarder
		contentPane.add(controlPanel,BorderLayout.EAST);//add the control to the east of the window boarder
		level = Utils.DEFAULT_GAME_LEVEL;//set the level of the game as default level
		score = 0;//set score as 0
		gameOver = false;//the game is still in              
		
		setSize(800, 600);
		setVisible(true);
	}
        
	//start the game
	public void startGame(){
		resetScore();//reset Score
		resetLevel();//reset Level
		gameOver = false;
		snake = new Snake(gameCanvas,this);//create Snake thread
		snake.start();//start Snake thread
                
	}

	//pause the game
	public void pauseGame(){
	  	snake.pauseMove();
	}

	//resume the game
	public void resumeGame(){
		snake.resumeMove();
	}

	//stop the game
	public void stopGame(){
		snake.stopMove();
	}

	//game over
	public void gameOver(){
		gameOver = true;
		//show the game over message dialog
		JOptionPane.showMessageDialog(null, "Game Over", "Game Over",
				JOptionPane.INFORMATION_MESSAGE);
		gameCanvas.resetCanvas();
	}

	//check if the game is over
	public boolean isGameOver(){
		return gameOver;
	}
	
	//check if the game is playing
	public boolean isPlaying(){
		return snake.isRunning();
	}

	//get the level of the game
	public int getLevel() {
		return level;
	}

        //get currtently game's score
	public int getScore() {
		return score;
	}

	//check if the game should update the level
	public boolean shouldIncLevel() {
		return score % Utils.PER_LEVEL_SCORE == 0;
	}

	//update the level
	public void incLevel(){
		if(canIncLevel()){
			level++;
			snake.incSpeed();
		}
	}

	//lower the level
	public void decLevel(){
		if(canDecLevel()){
			level--;
			snake.decSpeed();
		}
	}

	//increase the score of the game everytime when the snake eat a food
	public void incScore(){
	        score += Utils.PER_FOOD_SCORE;	
	}

	 //change Snake's movement  Direction
	public void changeSnakeDirection(int direction){
		snake.changeDirection(direction);
	}

	//get Snake Direction
	public int getSnakeDirection(){
		return snake.getDirection();
	}

	//check if can upgrade the level otherwise the highest level　
	private boolean canIncLevel(){
		return level < Utils.MAX_GAME_LEVEL;
	}

	//check if should lower the level otherwise it is the lowest level
	private boolean canDecLevel(){
		return level > 1;
	}

	// reset Score
	private void resetScore(){
		score = 0;
	}

	//reset the level of the game
	private void resetLevel(){
		level = Utils.DEFAULT_GAME_LEVEL;
	}
	
	public static void main(String []args){
                
		SnakeGame game = new SnakeGame();//create a game
		game.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
}
